package com.hope.onlinestudy.activity.chanagepass

import android.text.TextUtils
import com.google.gson.Gson
import com.hope.onlinestudy.activity.chanagepass.ChanagePassContract.Presenter
import com.hope.onlinestudy.base.BaseDataModel
import com.hope.onlinestudy.mvp.BasePresenterImpl
import com.hope.onlinestudy.net.HttpNetUtils
import com.hope.onlinestudy.net.NetworkScheduler
import com.hope.onlinestudy.net.ProgressSubscriber
import com.hope.onlinestudy.utils.ApiUtils
import com.hope.onlinestudy.utils.ApiUtils.loginModel

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class ChanagePassPresenter :
    BasePresenterImpl<ChanagePassContract.View>(), Presenter {

    override fun changepass(oldPass: String?, newPass: String?, subPass: String?) {
        if (TextUtils.isEmpty(oldPass)) {
            mView?.showMsg("原密码不能为空")
            return
        }
        if (TextUtils.isEmpty(newPass)) {
            mView?.showMsg("新密码不能为空")
            return
        }
        if (TextUtils.isEmpty(subPass)) {
            mView?.showMsg("重复密码不能为空")
            return
        }

        if (!TextUtils.equals(newPass, subPass)) {
            mView?.showMsg("两次密码不一致")
            return
        }
        val msg = Gson().toJson(
            hashMapOf<String, Any>(
                "newpassword" to newPass!!,
                "oldpassword" to oldPass!!,
                "userId" to loginModel?.userId!!
            )
        )
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.changePass}"
        HttpNetUtils.getInstance().getManager()?.changepass(url, loginModel?.jwtToken!!, msg)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<BaseDataModel<String>>(mView) {
                override fun onSuccess(data: BaseDataModel<String>?, code: Int?) {
                    if (data?.statusCode == 0) {
                        mView?.showMsg("密码修改成功")
                    } else {
                        mView?.showMsg("密码修改失败")
                    }
                }
            })
    }
}